package com.content.Service.Impl.show;

import com.common.Utils.JwtUtil;
import com.content.Entity.show.Program;
import com.content.Entity.show.ProgramType;
import com.content.Entity.show.UpdateOrder;
import com.content.Mapper.show.ProgramMapper;
import com.content.Service.show.ProgramService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Random;

@Service
public class ProgramServiceImpl implements ProgramService {
    @Autowired
    private ProgramMapper programMapper;

    @Override
    public PageInfo<Program> getProgramList(Integer pageNum, Integer pageSize, String programName, String showId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Program> programList = programMapper.getProgramList(programName,showId);
        return new PageInfo<>(programList);
    }

    @Override
    public void addProgram(Program program) {
        String token=program.getToken();
        String username= JwtUtil.extractUsername(token);
        program.setCreateUser(username);
        program.setProgramId(makeProgramId());
        program.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        programMapper.addProgram(program);

    }

    @Override
    public void updateProgram(Program program) {
        String token=program.getToken();
        String username= JwtUtil.extractUsername(token);
        program.setUpdateUser(username);
        program.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        programMapper.updateProgram(program);
    }

    @Override
    public void deleteProgram(String programId) {
        programMapper.deleteProgram(programId);

    }

    @Override
    public PageInfo<ProgramType> getProgramTypeList(Integer pageNum, Integer pageSize, String programTypeName) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProgramType> programList = programMapper.getProgramTypeList(programTypeName);
        return new PageInfo<>(programList);
    }

    @Override
    public void addProgramType(ProgramType programType) {
        String token = programType.getToken();
        String username = JwtUtil.extractUsername(token);
        programType.setCreateUser(username);
        programType.setProgramTypeId(makeProgramTypeId());
        programType.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        programMapper.addProgramType(programType);
    }

    @Override
    public void updateProgramType(ProgramType programType) {
        String token = programType.getToken();
        String username = JwtUtil.extractUsername(token);
        programType.setUpdateUser(username);
        programType.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        programMapper.updateProgramType(programType);

    }

    @Override
    public void deleteProgramType(String programTypeId) {
        programMapper.deleteProgramType(programTypeId);

    }

    @Override
    public List<ProgramType> getProgramTypeLists() {
        return programMapper.getProgramTypeLists();
    }

    @Override
    public void updateProgramOrder(UpdateOrder updateOrder) {
        String token = updateOrder.getToken();
        String username = JwtUtil.extractUsername(token);
        updateOrder.setUpdateUser(username);
        updateOrder.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));

        programMapper.updateProgramOrder(updateOrder);

    }

    @Override
    public List<Program> getProgramListById(String showId) {
        return programMapper.getProgramListById(showId);
    }

    public  String makeProgramId(){
        long timestamp = System.currentTimeMillis();
        String random = String.format("%04d", new Random().nextInt(1000));
        return "P" + timestamp + random;
    }

    public  String makeProgramTypeId(){
        long timestamp = System.currentTimeMillis();
//        String random = String.format("%04d", new Random().nextInt(1000));
//        return "PT" + timestamp + random;
        return "PT" + timestamp;
    }
}
